<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="学届" prop="yearType">
                <el-select
                    v-model="queryParams.yearType"
                    placeholder="请选择学届"
                    clearable
                >
                    <el-option
                        v-for="dict in dict.type.stu_year_list"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="课程名称" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入课程名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="老师姓名" prop="teacher">
                <el-input
                    v-model="queryParams.teacher"
                    placeholder="请输入老师姓名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>
        <TableCard>
            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="lessonList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="学届"
                    align="center"
                    width="80"
                    prop="yearType"
                >
                    <template slot-scope="scope">
                        <dict-tag
                            :options="dict.type.stu_year_list"
                            :value="scope.row.yearType"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="课程名称" align="center" prop="name" />
                <el-table-column
                    label="老师姓名"
                    align="center"
                    prop="teacherName"
                />
                <el-table-column
                    label="成绩权重"
                    align="center"
                    prop="scoreScale"
                />
                <el-table-column
                    label="作业权重"
                    align="center"
                    prop="jobScale"
                />
                <el-table-column
                    label="实验权重"
                    align="center"
                    prop="expScale"
                />
                <!--            <el-table-column label="备注" align="center" prop="remark"/>-->
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    width="800"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="importScore(scope.row, 'score')"
                            v-hasPermi="['stu:lesson:edit']"
                            >导入考试成绩</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="importScore(scope.row, 'job')"
                            v-hasPermi="['stu:lesson:edit']"
                            >导入作业成绩</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="importScore(scope.row, 'exp')"
                            v-hasPermi="['stu:lesson:edit']"
                            >导入实验成绩</el-button
                        >

                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="studentScore(scope.row, 'score')"
                            v-hasPermi="['stu:lesson:edit']"
                            >查看考试成绩</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="studentScore(scope.row, 'job')"
                            v-hasPermi="['stu:lesson:edit']"
                            >查看作业成绩</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="studentScore(scope.row, 'exp')"
                            v-hasPermi="['stu:lesson:edit']"
                            >查看实验成绩</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <el-dialog
            :title="upload.title"
            :visible.sync="upload.open"
            width="400px"
            append-to-body
        >
            <el-upload
                ref="upload"
                :limit="1"
                accept=".xlsx, .xls"
                :headers="upload.headers"
                :action="
                    upload.url +
                    '?lessonId=' +
                    upload.lessonId +
                    '&type=' +
                    upload.type
                "
                :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress"
                :on-success="handleFileSuccess"
                :auto-upload="false"
                drag
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或<em>点击上传</em>
                </div>
                <div class="el-upload__tip text-center" slot="tip">
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-input v-model="upload.type" v-show="false" />
                    <el-link
                        type="primary"
                        :underline="false"
                        style="font-size: 12px; vertical-align: baseline"
                        @click="importTemplate"
                        >下载模板</el-link
                    >
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm"
                    >确 定</el-button
                >
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listLesson,
    getLesson,
    delLesson,
    addLesson,
    updateLesson,
} from "@/api/stu/lesson";
import { listUser } from "@/api/system/user";
import { getToken } from "@/utils/auth";

export default {
    name: "Lesson",
    dicts: ["stu_year_list"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 课程表格数据
            lessonList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                name: null,
                teacher: null,
                teacherId: null,
                inspectPointId: null,
                inspectPointName: null,
                scoreScale: null,
                jobScale: null,
                expScale: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            userList: [],
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                lessonId: null,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/stu/score/importData",
            },
        };
    },
    created() {
        this.getList();
        this.getUserList();
    },
    methods: {
        /** 查询课程列表 */
        getList() {
            this.loading = true;
            listLesson(this.queryParams).then((response) => {
                this.lessonList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getUserList() {
            listUser().then((res) => {
                if (res) {
                    this.userList = res.rows;
                }
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                teacher: null,
                teacherId: null,
                inspectPointId: null,
                inspectPointName: null,
                scoreScale: null,
                jobScale: null,
                expScale: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        importScore(row, type) {
            this.upload.title = "用户导入";
            this.upload.lessonId = row.id;
            this.upload.type = type;
            this.upload.open = true;
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
                    response.msg +
                    "</div>",
                "导入结果",
                { dangerouslyUseHTMLString: true }
            );
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },

        //学生成绩
        studentScore(row, type) {
            this.$router.push({
                path: "/stu/score/studentScore/" + row.id,
                query: { type: type },
            });
        },
        /** 下载模板操作 */
        importTemplate() {
            window.open(
                process.env.VUE_APP_BASE_API +
                    "/profile/excelTemplate/score_import_template.xlsx"
            );
        },
    },
};
</script>
